<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function sum(...args) {
            var total = 0;
            for (item of args) {
                total += item;
            }
            function s(...args) {
                for (item of args) {
                    total += item;
                }
                //console.log(total);
                //s.value=total;
                return s;
            }
            s.toString = function () {
                return total;
            }
            return s;
        }
        console.log(sum(1, 2, 3));
        console.log(sum(1)(2)(3));
        console.log(sum(1, 2)(30));//6
    </script>
</body>

</html>